;; The first four lines of this file were added by Dracula.
;; They tell DrScheme that this is a Dracula Modular ACL2 program.
;; Leave these lines unchanged so that DrScheme can properly load this file.
#reader(planet "reader.rkt" ("cce" "dracula.plt") "modular" "lang")
(require "IThreshold.lisp")
(require "iimage.lisp")
(require "icommon.lisp")

(module MThreshold
  (include-book "list-utilities" :dir :teachpacks)
  (include-book "io-utilities" :dir :teachpacks)
  (import IImage)
  (import ICommon)
  
  ;function checks if one pixel is less than the threshold passed by user
  (defun pixelLessThanThreshold (pixel threshold)
    (if (<= (car pixel) (car threshold))
       (if (<= (cadr pixel) (cadr threshold))
            (if (<= (caddr pixel)(caddr threshold))
                t
                nil)
            nil)
        nil))
  
  ;function replaces all the pixels that are less than the threshold value with the white pixel.
  (defun processThreshOld (tree thresholdValue x y treeWidth treeHeight)
    (let* ((nextX (getNextXCoordinate x treeWidth))
           (nextY (getNextYCoordinate x y treeWidth))
           (pixel (getPixel tree x y))
           (newPixelVal (if (pixelLessThanThreshold pixel thresholdValue) (createRGB 255 255 255) pixel))
           (newPixRVal (car newPixelVal))
           (newPixGVal (cadr newPixelVal))
           (newPixBVal (caddr newPixelVal))) 
      (if (isValidPixel nextX nextY treeWidth treeHeight)
          (processThreshold (insertPixel tree x y newPixRVal newPixGVal newPixBVal) thresholdValue nextX nextY treeWidth treeHeight)
          (insertPixel tree x y newPixRVal newPixGVal newPixBVal))))
  
  (defun MainThreshold (tree args)
    (let* ((r (getElement args 1))
           (g (getElement args 2))
           (b (getElement args 3))
           (thresholdValue (createRGB r g b))
           (rgbWithinRange (validPixel thresholdValue))
           (message '("The Threshold presented is not valid"))
           (treeWidth (getWidth tree))
           (treeHeight (getHeight tree)))
      (if rgbWithinRange
          (mv (processThreshold tree thresholdValue 1 treeHeight treeWidth treeHeight) (list "Successfully applied threshold to image."))
          (mv tree message))))
  
  (export IThreshold))